#!usr/bin/env python
#coding=utf8

import re

from django.core.exceptions import ObjectDoesNotExist

from tuangou.models.tuan_models import CooperationPlatform, CooperationBrand
from tuangou.common_logic import commutil
    
def get_platform_list(order_by='index'):
    platforms = CooperationPlatform.objects.order_by(order_by).all()
    
    return platforms

def get_platform_home_list(order_by='index'):
    platforms = CooperationPlatform.objects.filter(showhomepage=True).order_by(order_by).all()
    
    return platforms

def get_platform_count():
    return CooperationPlatform.objects.count()

def get_platform_by_id(id):
    try:
        platform = CooperationPlatform.objects.get(id=id)
    except:
        platform = None
        
    return platform

def delete_platform(id):
    CooperationPlatform.objects.filter(id=id).delete()

def save_platform(**kwargs):
    platform = CooperationPlatform(**kwargs)
    platform.save()
    return platform

def update_platform(id, **kwargs):
    platform = get_platform_by_id(id)
    if platform:
        platform.update(**kwargs)
    return platform

def get_brand_list(order_by='index'):
    brands = CooperationBrand.objects.order_by(order_by).all()
    
    return brands

def get_brand_home_list(order_by='index'):
    brands = CooperationBrand.objects.filter(showhomepage=True).order_by(order_by).all()
    
    return brands

def get_brand_count():
    return CooperationBrand.objects.count()

def delete_brand(id):
    CooperationBrand.objects.filter(id=id).delete()

def save_brand(**kwargs):
    brand = CooperationBrand(**kwargs)
    brand.save()
    return brand

def update_brand(id, **kwargs):
    brand = get_brand_by_id(id)
    if brand:
        brand.update(**kwargs)
    return brand

def get_brand_by_id(id):
    try:
        brand = CooperationBrand.objects.get(id=id)
    except:
        brand = None
        
    return brand



